<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/28
 * Time: 21:01
 */

namespace Zch\LishangLaravelShop\Wap\Member\Provides;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Arr;
use EasyWeChat\OfficialAccount\Application as OfficialAccount;

class MemberServiceProvider extends ServiceProvider
{
    // member组件需要注入的中间件
    protected $routeMiddleware = [
        'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
    ];

    protected $middlewareGroups = [];

    protected $memberCommands = [
        \Zch\LishangLaravelShop\Wap\Member\Console\Commands\wapMemberInstall::class
    ];

    /*
     加载视图
    $this->loadViewsFrom(
        __DIR__ . '/../../resources/views/', 'Zchjunit'
    );
    加载自定义配置文件
    protected function mergeConfigFrom($path, $key)
    {
        $config = $this->app['config']->get($key, []);

        $this->app['config']->set($key, array_merge(require $path, $config));
    }
   */

    public function register()
    {
        $this->registerRoutes();
        $this->mergeConfigFrom(__DIR__ . '/../Config/member.php', 'wap.member');
        $this->registerRouteMiddleware();
        $this->publishConfigFile();
        $this->loadMemberAuthConfig();

    }

    public function boot()
    {
        $this->loadMigrations();
        //加载命令
        $this->commands($this->memberCommands);
        //加载配置
        $this->app->singleton("wechat.official_account.default", function ($laravelApp) {
            $app = new OfficialAccount(array_merge(config('wechat.official_account.default', []), config('wechat.official_account')));
            if (config('wechat.defaults.use_laravel_cache')) {
                $app['cache'] = $laravelApp['cache.store'];
            }
            $app['request'] = $laravelApp['request'];
            return $app;
        });
    }

    //发布配置文件，命令如下
    //php artisan vendor:publish --provider="Zch\LishangLaravelShop\Wap\Member\Provides\MemberServiceProvider"
    public function publishConfigFile($value = '')
    {
        //判断是否在命令行中运行
        if($this->app->runningInConsole()){
             //当前组件的目录 => 要发布到的目录,为空则创建文件，否则创建文件夹，如现在会在config下创建wap文件夹,里面有member.php文件
             $this->publishes([__DIR__ . '/../Config/' => config_path('wap')], 'wap.member');
        }
    }

    protected function loadMemberAuthConfig()
    {
        //把组件的配置信息加入到你的项目eswechat组件配置文件中
        config(Arr::dot(config('wap.member.wechat', []), 'wechat.'));
        config(Arr::dot(config('wap.member.auth', []), 'auth.'));
    }

    private function routeConfiguration(){
        return [
            'namespace' => 'Zch\LishangLaravelShop\Wap\Member\Http\Controllers',
            'prefix' => 'wap/member',
            'middleware' => 'web'
        ];
    }

    public function registerRoutes()
    {
        Route::group($this->routeConfiguration(),function(){
            $this->loadRoutesFrom(__DIR__ .  '/../Http/Routes.php');
        });
    }

    protected function registerRouteMiddleware()
    {
        foreach ($this->middlewareGroups as $key => $middleware) {
            $this->app['router']->middlewareGroup($key, $middleware);
        }

        foreach ($this->routeMiddleware as $key => $middleware) {
            $this->app['router']->aliasMiddleware($key, $middleware);
        }
    }

    //加载组件的数据库迁移文件
    public function loadMigrations($value = '')
    {
        if($this->app->runningInConsole()){
            $this->loadMigrationsFrom(__DIR__ . '/../Database/migrations');
        }
    }

    /*
     * protected function loadMigrationsFrom($paths)
        {
            $this->app->afterResolving('migrator', function ($migrator) use ($paths) {
                foreach ((array) $paths as $path) {
                    $migrator->path($path);
                }
            });
        }
     * */
}
